#include <stdio.h>
#define max 1003

char str[max][max];
int count[max],m,n;
typedef struct nod{
    int num;
    char s[max];
    int thutu;    
    int ko;
} node;
node cus[max];

int sosanh(int j,int i)
{
    int k=0;
    while (str[j][k]!='\0')
    if (str[j][k]==cus[i].s[k]) k++;
    else return 0;
    return 1;
}

void nhap()
{
    int i,j,k;
    scanf("%d",&m);
    char temp[max];
    for (i=0;i<m;i++)
    {
        scanf("%d",&count[i]);
        scanf("%c%c%c%c",&k,&k,&k,&k);
        scanf("%s",str[i]);
    }
    scanf("%d",&n);
    for (i=0;i<n;i++)
    {
        scanf("%d",&cus[i].num);
        scanf("%c%c%c%c",&k,&k,&k,&k);
        scanf("%s",cus[i].s);
        cus[i].thutu=-1;
        cus[i].ko=0;
        for (j=0;j<m;j++) if (sosanh(j,i))
        {
            cus[i].thutu=j;
            break;
        }
        if (cus[i].thutu==-1) cus[i].ko=1;
        else cus[i].ko=0;
        
    }
}

void swap1(int i)
{
    node tg;
    tg=cus[i];
    cus[i]=cus[i+1];
    cus[i+1]=tg;
}

void xuly()
{
    int i=0,t;
    long dem=0;
    cus[n].ko=1;
    while (i<n)
    {
        t=cus[i].num;
        if (cus[i].ko)
        {
            dem++;
            i++;
        }
        else if (count[cus[i].thutu]==0)
        {
            dem++;
            i++;
        }
        else if (t<=count[cus[i].thutu])
        {
            dem++;
            count[cus[i].thutu]-=t;
            i++;
        }
        else if (t>count[cus[i].thutu])
        {
            cus[i].num=count[cus[i].thutu];
            swap1(i);
            dem++;
        }
    }
    printf("%d",dem);
}

int main()
{
    nhap();
    xuly();
    return 0;
}
